package com.beadhouse.nurcontent.support.servcie;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.beadhouse.common.dto.QueryResult;
import com.beadhouse.common.exeption.BeadHouseException;
import com.beadhouse.entity.NurContentEntity;
import com.beadhouse.entity.UserEntity;
import com.beadhouse.nurcontent.ao.NurContentAo;
import com.beadhouse.nurcontent.dao.NurContentDao;
import com.beadhouse.nurcontent.dto.NurContentModifyDto;
import com.beadhouse.nurcontent.service.NurContentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.NonNull;
@Service
public class NurContentServiceImp implements NurContentService{
	@Autowired
	NurContentDao ncd;
	@Override
	public void addNurContent(@NonNull NurContentAo nca,int currentId) throws Exception {
		
		NurContentEntity nce=new NurContentEntity();
		nce.setCreatBy(Integer.toString(currentId));
		nce.setPrice(nca.getPrice());
		nce.setCreatDate(new Date());
		nce.setDelFlag(false);
		nce.setDescribe(nca.getDescribe());
		nce.setIncrementFlag("1".equals(nca.getIncrement()));
		nce.setName(nca.getName());
		nce.setRemarks(nca.getRemarks());
		nce.setSerlalNumber(nca.getSerlalNumber());
		nce.setStatus("1".equals(nca.getStatus()));
		nce.setUpdateBy(null);
		nce.setUpdateDate(null);
		
		if(ncd.exists("serlal_number",nca.getSerlalNumber())) {
			throw new BeadHouseException("编号已存在");
		}
		ncd.insert(nce);
		
	}
	@Override
	public void deleteNurContent(@NonNull @Max(11) int id,int currentId,Map<String,Object>map) throws Exception {
		boolean exists=ncd.exists("id",id);
		if(!exists) {
			throw new BeadHouseException("id不存在");
		}
		ncd.update(id, map);
	}
	@Override
	public QueryResult<NurContentEntity> selectNurContent(String key,int pageNo) throws Exception {
		if(key!=null) {
			if(key.length()!=0) {
				key="%"+key+"%";
			}else {
				key=null;
			}
		}
		PageHelper.startPage(pageNo,5);
		List<NurContentEntity> rows= ncd.selectByLike(key);
		PageInfo<NurContentEntity> page = new PageInfo<>(rows);
		//组装业务结果
		QueryResult<NurContentEntity> result=new QueryResult<NurContentEntity>();
		
		result.setPageNo(1);
		result.setPageSize(5);
		result.setRows(rows);
		result.setTotalRows((int)page.getTotal());
		
		return result;
	}
	@Override
	public void updateNurContent(int id,@NotNull Map<String,Object> map) throws Exception {
	
		NurContentEntity old=ncd.select("id", id);
		if(old==null) {
			throw new BeadHouseException("用户不存在");
		}
		String name=old.getName();
		String price=old.getPrice();
		String remarks=old.getRemarks();
		Boolean incrementFlag=old.getIncrementFlag();
		Boolean status=old.getStatus();
		String describe=old.getDescribe();
		if(name!=null) {
			if(name.equals(map.get("name"))) {
				map.remove("name");
			}
		}
		if(price!=null) {
			if(price.equals(map.get("price"))) {
				map.remove("price");
			}
		}
		if(remarks!=null) {
			if(remarks.equals(map.get("remarks"))) {
				map.remove("remarks");
			}
		}
		if(incrementFlag!=null) {
			if(incrementFlag.equals(map.get("incrementFlag"))) {
				map.remove("incrementFlag");
			}
		}
		if(status!=null) {
			if(status.equals(map.get("status"))) {
				map.remove("status");
			}
		}
		if(describe!=null) {
			if(describe.equals(map.get("describe"))) {
				map.remove("describe");
			}
		}
		ncd.update(id, map);
	}
	@Override
	public NurContentModifyDto selectOne(int id) throws Exception {
		boolean exists=ncd.exists("id",id);
		if(!exists) {
			throw new BeadHouseException("id不存在");
		}
		NurContentEntity nce=ncd.select("id",id);
		NurContentModifyDto ncmd=new NurContentModifyDto();
		ncmd.setIncrementFlag(nce.getIncrementFlag());
		ncmd.setName(nce.getName());
		ncmd.setNurDescribe(nce.getDescribe());
		ncmd.setPrice(nce.getPrice());
		ncmd.setRemarks(nce.getRemarks());
		ncmd.setStatus(nce.getStatus());
		return ncmd;
	}

}
